//
//  GuideOfInfo.m
//  Project_A
//
//  Created by dllo on 16/1/28.
//  Copyright © 2016年 dllo. All rights reserved.
//

#import "GuideOfzhuyi.h"
#import "ModelOfzhuyi.h"
#import "ViewOfzhuyiHeader.h"
#import "CellOfzhuyi.h"
#import "CellOfzhuyi2.h"
#import <UIImageView+WebCache.h>


#define WIDTH [UIScreen mainScreen].bounds.size.width

@interface GuideOfzhuyi ()<UITableViewDataSource,UITableViewDelegate>
@property (nonatomic, retain) NSMutableDictionary *dic;
@property (nonatomic, retain) NSArray *array;
@property (nonatomic, retain) UITableView *tableView;


@property (nonatomic,assign)NSInteger number;

@property (nonatomic,assign)NSInteger widnumber;
@end

@implementation GuideOfzhuyi
- (void)dealloc {
    [_arrpass release];
    [_array release];
    [_tableView release];
    [_dic release];
    
    [super dealloc];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    [self handleData];
    [self createTableView];


}

#pragma mark - handleData

- (void)handleData {
    self.dic = [NSMutableDictionary dictionary];
    for (NSDictionary *dic in self.arrpass) {
        NSString *key = [dic objectForKey:@"title"];
        NSArray *arrSections = [dic objectForKey:@"sections"];
        NSMutableArray *array = [NSMutableArray array];
        for (NSDictionary *dicModel in arrSections) {
            ModelOfzhuyi *model = [[ModelOfzhuyi alloc] init];
            [model setValuesForKeysWithDictionary:dicModel];
            model.descriptions = [dicModel objectForKey:@"description"];
            model.photos = [dicModel objectForKey:@"photos"];
            [array addObject:model];
            [model release];
        }
        [self.dic setObject:array forKey:key];
        
    }
    
    self.array = [self.dic allKeys];
}

#pragma mark - tableView
- (void)createTableView {
    self.tableView = [[UITableView alloc] initWithFrame:self.view.frame style:UITableViewStylePlain];
    [self.view addSubview:self.tableView];
    self.tableView.delegate =self;
    self.tableView.dataSource = self;
    [self.tableView registerClass:[CellOfzhuyi class]forCellReuseIdentifier:@"CellOfzhuyi"];
    
    [self.tableView  registerClass:[CellOfzhuyi2 class] forCellReuseIdentifier:@"CellOfzhuyi2"];
    
    [_tableView release];
    
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    NSString *key = [self.array objectAtIndex:section];
    NSArray *arr = [self.dic objectForKey:key];
    return arr.count;
    
}
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
    NSString *key = [self.array objectAtIndex:section];
    
//    UIView *view = [[UIView alloc]initWithFrame:CGRectMake(0, 0, WIDTH, 40)];
//    UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, WIDTH, 40)];
//    [view addSubview:label];
//    
//    label.text = key;
//    
//    label.font = [UIFont fontWithName:@"Arial-BoldMT" size:25];
//
    
    
    
    NSLog(@"%@",key);
    
    return key;
    
}




- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    
    return self.array.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    NSString *key = [self.array objectAtIndex:indexPath.section];
    NSArray *arr = [self.dic objectForKey:key];
    
     ModelOfzhuyi *model = [arr objectAtIndex:indexPath.row];
    
    if (model.photos.count == 0) {
        CellOfzhuyi *cell = [tableView dequeueReusableCellWithIdentifier:@"CellOfzhuyi"];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        tableView.separatorStyle = NO;
        
        
        [cell passAnyModel:model];
        
        return cell;

    }else{
        CellOfzhuyi2 *cell = [tableView dequeueReusableCellWithIdentifier:@"CellOfzhuyi2"];
        cell.selectionStyle = UITableViewCellSelectionStyleBlue;
        tableView.separatorStyle = NO;
        
        
        
        [cell passAnyModel:model];
        cell.photos = model.photos;
        NSInteger Z = 0;
        
        for (NSDictionary *Dic in cell.photos) {
            
            NSInteger w = [[Dic objectForKey:@"image_width"]integerValue];
//            NSLog(@"宽%ld",w);
            
            NSInteger h = [[Dic objectForKey:@"image_height"]integerValue];
            
            NSInteger S = w * (WIDTH - 20) / (2 * h);
            
//            NSLog(@"%ld",S);
            
            self.number = S;
            
            
            
            Z = Z + S;
            
            UIImageView *image = [[UIImageView alloc]init];
            
            NSString *str = [Dic objectForKey:@"image_url"];
            
            [image sd_setImageWithURL:[NSURL URLWithString:str] placeholderImage:nil options:SDWebImageAvoidAutoSetImage];
            
            image.layer.borderWidth = 2;
            image.layer.borderColor = [[UIColor whiteColor]CGColor];
            
            image.layer.masksToBounds = YES;
            image.frame = CGRectMake(Z- self.number, 0, self.number, WIDTH / 2);
            
            [cell.scroll addSubview:image];
            
            [image release];

            
    }        
        cell.scroll.contentSize = CGSizeMake(Z ,(WIDTH - 20)*0.5);
        
        cell.scroll.bounces = NO;
          NSLog(@"%ld",cell.photos.count);
        
        return cell;
    }
    
    
};

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    NSString *key = [self.array objectAtIndex:indexPath.section];
    NSArray *arr = [self.dic objectForKey:key];
    
    ModelOfzhuyi *model = [arr objectAtIndex:indexPath.row];
    
    
    if (model.photos.count == 0) {
       
        
        return [CellOfzhuyi heightForLabel:model.descriptions] + 60;
        
    }else{
        
        
        return [CellOfzhuyi heightForLabel:model.descriptions] + 60 + [UIScreen mainScreen].bounds.size.width * 0.5;
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
